const fs = require('fs');
const path = require('path');

// 需要更新的实体文件列表
const entityFiles = [
    'nestjs-app/src/entities/user.entity.ts',
    'nestjs-app/src/entities/character.entity.ts',
    'nestjs-app/src/entities/area.entity.ts',
    'nestjs-app/src/entities/character-attribute.entity.ts',
    'nestjs-app/src/entities/character-detailed-attribute.entity.ts',
    'nestjs-app/src/entities/character-skill.entity.ts',
    'nestjs-app/src/entities/character-religion.entity.ts',
    'nestjs-app/src/entities/character-residence.entity.ts',
    'nestjs-app/src/entities/character-stone.entity.ts',
    'nestjs-app/src/entities/character-resources.entity.ts',
    'nestjs-app/src/entities/character-inventory.entity.ts',
    'nestjs-app/src/entities/monthly-card.entity.ts',
    'nestjs-app/src/entities/character-location.entity.ts',
    'nestjs-app/src/entities/character-gem-inventory.entity.ts',
    'nestjs-app/src/entities/equipment-gem-socket.entity.ts',
    'nestjs-app/src/entities/character-attribute-points.entity.ts',
    'nestjs-app/src/entities/character-equipment.entity.ts',
    'nestjs-app/src/entities/character-training.entity.ts',
    'nestjs-app/src/entities/mount-training.entity.ts',
    'nestjs-app/src/entities/shop-goods.entity.ts',
    'nestjs-app/src/entities/faction-member.entity.ts',
    'nestjs-app/src/entities/gem-mining.entity.ts',
    'nestjs-app/src/entities/deputy-general-equipment.entity.ts',
    'nestjs-app/src/entities/deputy-general-stone.entity.ts',
    'nestjs-app/src/entities/announcement.entity.ts',
    'nestjs-app/src/entities/chat-message.entity.ts',
    'nestjs-app/src/entities/system-message.entity.ts',
    'nestjs-app/src/entities/item.entity.ts',
    'nestjs-app/src/entities/private-message.entity.ts',
    'nestjs-app/src/entities/basic-item.entity.ts',
    'nestjs-app/src/entities/equipment-base.entity.ts',
    'nestjs-app/src/entities/gem-base.entity.ts',
    'nestjs-app/src/entities/gem-attribute.entity.ts',
    'nestjs-app/src/entities/mount-base.entity.ts',
    'nestjs-app/src/entities/social-relation.entity.ts',
    'nestjs-app/src/entities/player-online-time.entity.ts',
    'nestjs-app/src/entities/npc.entity.ts',
    'nestjs-app/src/entities/site.entity.ts',
    'nestjs-app/src/entities/map-area.entity.ts',
    'nestjs-app/src/entities/talent-stone.entity.ts',
    'nestjs-app/src/entities/deputy-general.entity.ts',
    'nestjs-app/src/entities/casino-dice-round.entity.ts',
    'nestjs-app/src/entities/casino-chibi-round.entity.ts',
    'nestjs-app/src/entities/casino-dice-bet.entity.ts',
    'nestjs-app/src/entities/casino-chibi-bet.entity.ts',
    'nestjs-app/src/entities/casino-config.entity.ts',
    'nestjs-app/src/entities/casino-operation-log.entity.ts',
    'nestjs-app/src/entities/casino-ranking.entity.ts',
    'nestjs-app/src/entities/casino-rock-paper-scissors.entity.ts'
];

function updateEntityFile(filePath) {
    if (!fs.existsSync(filePath)) {
        console.log(`⏭️ 跳过不存在的文件: ${filePath}`);
        return;
    }

    let content = fs.readFileSync(filePath, 'utf8');
    let modified = false;

    // 检查是否已经有BaseEntity导入
    if (content.includes('BaseEntity')) {
        console.log(`⏭️ 跳过已更新的文件: ${filePath}`);
        return;
    }

    // 添加BaseEntity导入
    if (content.includes('import {') && content.includes('from "typeorm"')) {
        content = content.replace(
            /import \{([^}]+)\} from "typeorm"/,
            (match, imports) => {
                if (!imports.includes('BaseEntity')) {
                    return `import {${imports}, BaseEntity} from "typeorm"`;
                }
                return match;
            }
        );
        modified = true;
    }

    // 添加BaseEntity导入（如果上面没有匹配到）
    if (!content.includes('BaseEntity') && content.includes('from "typeorm"')) {
        content = content.replace(
            /from "typeorm"/,
            'from "typeorm"'
        );
        // 添加BaseEntity导入
        content = content.replace(
            /import \{([^}]+)\} from "typeorm"/,
            'import {$1, BaseEntity} from "typeorm"'
        );
        modified = true;
    }

    // 添加BaseEntity导入（从本地文件）
    if (!content.includes('BaseEntity')) {
        content = content.replace(
            /import \{([^}]+)\} from "typeorm";/,
            'import {$1} from "typeorm";\nimport { BaseEntity } from "./base.entity";'
        );
        modified = true;
    }

    // 更新类定义，继承BaseEntity
    if (content.includes('export class') && !content.includes('extends BaseEntity')) {
        content = content.replace(
            /export class (\w+)/,
            'export class $1 extends BaseEntity'
        );
        modified = true;
    }

    // 移除手动定义的时间戳字段
    content = content.replace(
        /@Column\("datetime",\s*\{\s*name:\s*"created_at",\s*comment:\s*"创建时间"\s*\}\)\s*created_at!:\s*Date;\s*/g,
        ''
    );

    content = content.replace(
        /@Column\("datetime",\s*\{\s*name:\s*"updated_at",\s*comment:\s*"更新时间",\s*nullable:\s*true\s*\}\)\s*updated_at!:\s*Date\s*\|\s*null;\s*/g,
        ''
    );

    if (modified) {
        fs.writeFileSync(filePath, content);
        console.log(`✅ 更新文件: ${filePath}`);
    } else {
        console.log(`⏭️ 无需更新: ${filePath}`);
    }
}

console.log('🔧 开始更新实体文件以使用BaseEntity...');

entityFiles.forEach(updateEntityFile);

console.log('🎉 所有实体文件更新完成！');
